#include "csearchservice.h"
#include <QDebug>
CSearchService::CSearchService(QObject *parent) : QObject(parent)
{

}

void CSearchService::RecvOnlineFindRespond(std::shared_ptr<PDU> recvBag)
{
    QList<QPair<QString,QString>>recvData;

    char * InfoChar=new char[100000]{'\0'};
    memcpy(InfoChar,(char*)recvBag.get()+sizeof(PDU),recvBag->MsgLength);
    QJsonDocument dc=QJsonDocument::fromJson(QString(InfoChar).toUtf8());
    if(dc.isNull())return;
    if(!dc.isObject())return;
    delete [] InfoChar;
    InfoChar=nullptr;
    QJsonObject Body=dc.object();
    QJsonArray ARR=Body["ONLINEARRAY"].toArray();
    for(int i=0;i<ARR.size();i++){
        QPair<QString,QString>one;
        QJsonObject Jone=ARR[i].toObject();
        one.first=Jone["USER"].toString();
        one.second=Jone["ETC"].toString();
        recvData.push_back(one);
    }


    emit EmitOnlineFindResult(recvData);
}

void CSearchService::RecvAllNetScanRespond(std::shared_ptr<PDU> recvBag)
{
    QList<QPair<QString,QString>>recvData;

    char * InfoChar=new char[100000]{'\0'};
    memcpy(InfoChar,(char*)recvBag.get()+sizeof(PDU),recvBag->MsgLength);
    QJsonDocument dc=QJsonDocument::fromJson(QString(InfoChar).toUtf8());
    if(dc.isNull())return;
    if(!dc.isObject())return;
    delete [] InfoChar;
    InfoChar=nullptr;
    QJsonObject Body=dc.object();
    QJsonArray ARR=Body["ALLNETARRAY"].toArray();
    qDebug()<<recvData.size();
    for(int i=0;i<ARR.size();i++){
        QPair<QString,QString>one;
        QJsonObject Jone=ARR[i].toObject();
        one.first=Jone["USER"].toString();
        one.second=Jone["ETC"].toString();
        recvData.push_back(one);
    }
    qDebug()<<recvData;

    emit EmitAllNetScanAll(recvData);
}
